package com.jframe.sys.utils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class ClassUtil {

	/**
	 * 获取字段类型
	 * @param clz
	 * @param field
	 * @return
	 */
	public static String getFieldType(Class clz,String field){
		
		try {
			return clz.getDeclaredField(field).getType().getSimpleName();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	
	public static String getFieldType(Object obj,String field){
		return getFieldType(obj.getClass(),field);
	} 
	
	/**
	 * 类型转换
	 * @param clz
	 * @param field,将某个字段的值转换成对象字段对应的类型�?
	 * @param value
	 * @return
	 */
	public static Serializable typeTran(Class clz,String field,String value){
		
		String type = getFieldType(clz,field);
		if(type.equals("String")){			
			return value;
		}
		else if(type.equalsIgnoreCase("int") || type.equalsIgnoreCase("Integer")){
			return StringUtil.parseInt(value);
		}
		else if(type.equalsIgnoreCase("long")){
			return StringUtil.parseLong(value);
		}
		else if(type.equalsIgnoreCase("double")){
			return StringUtil.parseDouble(value);
		}
		else if(type.equalsIgnoreCase("float")){
			return StringUtil.parseFloat(value);
		}
		else if(type.equalsIgnoreCase("boolean")){
			return StringUtil.parseBoolean(value);
		}
		else if(type.equalsIgnoreCase("char")){
			return StringUtil.parseChar(value);
		}
		else{
			return value;
		}
	}
	public static Serializable typeTran(Object obj,String field,String value){
		return typeTran(obj.getClass(),field,value);
	}
	

	/**
	 * 将对象转换成map
	 * @param obj
	 * @return
	 */
	public static Map obj2Map(Object obj){
		String json = JsonUtil.obj2Json(obj);
		return JsonUtil.json2Map(json);
	}
	
	/**
	 * 获取指定class的所有字�?
	 * @param clz
	 * @return
	 */
	public static String[] getClzFields(Class clz){
		
	   
		Field[] fields=	 clz.getDeclaredFields(); //clz.getFields();
		String[] array = new String[fields.length];
		int i =0;
		for(Field field :fields){
			
			array[i++] = field.getName();
		}
		return array;
	}
	
	/**
	 * 获取指定Class的所有方�?
	 * @param clz
	 * @return
	 */
	public static String[] getClzMethods(Class clz){
		
		Method[] methods = clz.getMethods();
		String[] array = new String[methods.length];
		int i=0;
		for(Method method :methods){
			
		  array[i++] = method.getName();
		  System.out.println(array[i-1]);
		}
		return array;
	}
	
	public static String[] getClzMethods(String clz){		
		return getClzMethods(forName(clz));
	}
	
	public static Class forName(String clzName){
		
		Class clz = null;
		try {
			clz = Class.forName(clzName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clz;
	}
	
	public static Object setObjValue(Object obj,String field, Object value){
	   try{
		Method toMethod = obj.getClass().getMethod("set" + StringUtil.initLetterToUpperCase(field),
			obj.getClass().getDeclaredField(field).getType());		
		    toMethod.invoke(obj, value);
	   }
	   catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			return obj;
		}
	}
	
	/**
	 * 对象赋�?，将�?��对象的�?除id外，全部赋�?给另外一个对象�?
	 * @param to
	 * @param from
	 * @return
	 */
	public static Object copyValue(Object to,Object from){
		
		String[] fields= getClzFields(from.getClass());
		try{
		for(String field :fields){
			if(field.equalsIgnoreCase("id") ){
				continue;
			}
			else if(field.equalsIgnoreCase("serialVersionUID")){
				continue;
			}
			else{
				
				Method toMethod = to.getClass().getMethod("set" + StringUtil.initLetterToUpperCase(field),
						to.getClass().getDeclaredField(field).getType());
				Method fromMethod = from.getClass().getMethod("get" +StringUtil.initLetterToUpperCase(field));
				toMethod.invoke(to, fromMethod.invoke(from));
			}
		}
		
		}
		catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			return to;
		}
	}
	
	public static void main(String args[]){
		
		String clzName= "com.jframe.sys.portal.action.PageColManAction";
		getClzMethods(clzName);
		
	}
}
